<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <!-- 搜索 -->
    <div>
      <el-card shadow="always">
        <el-row :gutter="20">
          <el-col :span="7">
            <div>
              <el-input
                v-model="queryInfo.query"
                placeholder="请输入搜索内容"
                clearable
                class="input-with-select"
                @clear="queryUser"
              >
                <!-- <el-input placeholder="请输入搜索内容" v-model="input3" class="input-with-select"> -->
                <el-button slot="append" icon="el-icon-search" @click="getData"></el-button>
              </el-input>
            </div>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
          </el-col>
        </el-row>

        <!-- 表格区 -->
        <div>
          <el-table border :data="tableData" style="width: 100%">
            <el-table-column prop="id" label="#" width="100"></el-table-column>
            <el-table-column prop="username" label="姓名" width="200"></el-table-column>
            <el-table-column prop="email" label="邮箱" width="300"></el-table-column>
            <el-table-column prop="mobile" label="电话" width="300"></el-table-column>
            <el-table-column prop="role_name" label="角色" width="200"></el-table-column>
            <!-- 状态按钮 -->
            <el-table-column label="状态" width="180">
              <template slot-scope="scope">
                <!-- {{scope.row.mg_state}} -->
                <el-switch
                  v-model="scope.row.mg_state"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="updateStatus(scope.row.id,scope.row.mg_state)"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column prop="handle" label="操作">
              <!-- 编辑插槽 -->
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle @click="edit(scope.row)"></el-button>
                <el-button type="warning" icon="el-icon-s-tools" circle></el-button>
                <el-button type="danger" icon="el-icon-delete" circle @click="open(scope.row.id)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 分页区 -->
        <div class="block">
          <span class="demonstration"></span>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1, 5, 10, 20]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>

        <!-- 添加用户开始 -->
        <el-dialog
          title="添加用户"
          :visible.sync="dialogVisible"
          width="40%"
          :before-close="handleClose"
        >
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名称" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="用户密码" prop="password">
              <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="ruleForm.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="ruleForm.mobile"></el-input>
            </el-form-item>
            <!-- <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">立即添加</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>-->
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
          </span>
        </el-dialog>
        <!-- <div></div> -->
        <!-- 添加用户结束 -->

        <!-- 编辑开始 -->
        <el-dialog title="编辑用户" :visible.sync="editVisible" width="40%" :before-close="handleClose">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="editForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="editForm1.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="nemail">
              <el-input v-model="editForm1.nemail"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="editForm1.mobile"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="editVisible = false">取 消</el-button>
            <el-button type="primary" @click="editForm('editForm')">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
           
<script>
//导入接口
import { getUsers, status, addUser, editUser, delUser } from '../../api/index'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      tableData: [],
      total: 0,
      // 添加用户表单显示/隐藏
      dialogVisible: false,
      // 编辑用户表单显示/隐藏c
      editVisible: false,
      //添加用户数据
      id: 0,
      ruleForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      editForm1: {
        username: '',
        email: '',
        mobile: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }, { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 12, message: '长度在 6 到 12 位', trigger: 'blur' }],
        email: [{ required: false, type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }],
        // nemail: [{ required: false, type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }],
        mobile: [{ required: false, message: '请输入合法的手机号', trigger: 'blur' }, { min: 11, max: 15, message: '请输入合法的手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取数据渲染页面
    async getData() {
      // 此处传参为对象
      let { data: res } = await getUsers(this.queryInfo)
      if (res.meta.status !== 200) return this.$message.error('获取参数失败!')
      this.tableData = res.data.users
      // console.log(res.data)
      this.total = res.data.total
    },
    // 分页部分
    handleSizeChange(page) {
      this.queryInfo.pagesize = page
      this.getData()
    },
    handleCurrentChange(cur) {
      this.queryInfo.pagenum = cur
      this.getData()
    },
    // 修改用户状态
    async updateStatus(id, boo) {
      let data = await status(id, boo)
      this.getData()
    },
    queryUser() {
      this.getData()
    },

    //添加用户
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 添加用户表单填写
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          this.$message.error('表单内容填写有误！')
          return false
        }
        //formName 为表单名
        let { data: res } = await addUser(this.ruleForm)
        console.log(res)
        //返回值不是201 时则添加用户失败
        if (res.meta.status !== 201) {
          return this.$message.error(res.meta.msg)
        }
        this.$message({
          message: '添加用户成功',
          type: 'success'
        })
        this.dialogVisible = false
        this.resetForm('ruleForm')
        this.getData()
      })
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    //   //编辑表单，内容填充
    edit(row) {
      //row为插槽里的对象
      // console.log(row);
      this.editVisible = true
      this.id = Number(row.id)
      this.editForm1.email = row.email
      this.editForm1.username = row.username
      this.editForm1.mobile = row.mobile
    },
    // 编辑提交
    async editForm() {
      console.log(this.id)
      console.log(this.editForm1, 1111)
      let { data: res } = await editUser(this.id, { email: this.editForm1.email, mobile: this.editForm1.mobile })
      // let { data: res } = await editUser(this.id, this.editForm1)
      //返回值不是200 时则编辑用户失败
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message({
        message: '修改用户成功',
        type: 'success'
      })
      this.editVisible = false
      this.getData()
    },
    // 删除用户操作
    async open(id) {
      let confirm = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      if(confirm !== 'confirm'){
        return 
      }
      // 当用户点击了确定删除则发送删除请求
      let {data:res} = await delUser(id)
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message({
        message: '删除用户成功',
        type: 'success'
      })
      this.getData()
    }
  },
  // 监听添加表单的隐藏/显示，关闭时清空输入内容
  watch: {
    dialogVisible() {
      if (!this.dialogVisible) {
        this.resetForm('ruleForm')
      }
    },
    editVisible() {
      if (!this.editVisible) {
        this.resetForm('editForm')
      }
    }
  }
}
</script>
<style lang='less' scoped>
</style>